<?php
// +----------------------------------------------------------------------
// |地区控制器
// +----------------------------------------------------------------------
// |Data:2015-04-14 19:18
// +----------------------------------------------------------------------
// |Author: 吕志雄 <870923001@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\AdminCommonController;
use Common\Vendor\Common\Tree;
use Think\Auth;

class AreaController extends AdminCommonController{
    protected $_model;
    protected $separated = '&nbsp;&nbsp;&nbsp;&nbsp;';
    protected function _initialize(){
        parent::_initialize();
        $this->_model = D('Admin/Area');
    }

    /**
     * 列表
     */
    public function index(){
        $list = $this->_model->getAreaTreeData();

        $this->assign('html',showTreeCategory($list,'area',3,'name',false,false,false,true));

        $this->display();
    }

    /**
     * 添加或编辑
     */
    public function edit(){
        if(IS_POST){
            $name = I('post.name');
            $short = I('post.short');
            $area_code = I('post.area_code');
            $sortid = I('post.sort');
            $id = I('post.id',0);

            $data = array(
                "name" => $name,
                "short" => $short,
                "area_code" => $area_code,
                "sort" => $sortid,
            );
            if(!$id) {
                if ($id = $this->_model->add($data)) {
                    $this->clearCache();
                    $html = $this->get_area_html($id, $name);
                    $this->returnData('添加成功', 1, $html);
                } else {
                    $this->returnData('添加失败');
                }
            }else{
                if ($this->_model->update($data, $id)) {
                    $this->clearCache();
                    $this->returnData('修改成功', 1);
                }else{
                     $this->returnData('修改失败');
                }
            }
        }else{
            $id = I('get.id','');
            $type = I('get.type');//类型，1为增加分类，2为编辑分类
            if($id && $type){
                $list = $this->_model->getAreaTreeData();

                if($type==2) {
                    $data = M('area')->where('id=' . $id)->cache(5)->find();
                    $this->assign('nlist',$this->getChilds($list,$data['area_code']));
                    $this->assign('data', $data);
                }else{
                    $this->assign('nlist',$this->getChilds($list,$id));
                }
            }

            $this->assign('id', $id);
            $this->display('edit');
        }
    }

    /*
    * 获得所有自己且加修饰符
    * */
    public function getChilds($list,$area_code=""){
        $str = '';
        foreach($list as $value){
            if($area_code && $area_code == intval($value['area_code'])){
                $select = 'selected="selected"';
            }
            $str .= '<option value="'.$value['id'].'" '.$select.'>'.$this->getHierarchySeparator($value['area_code']).'└'.$value['name'].'</option>';
            $select = '';
            if($value['child']){
                $str .= $this->getChilds($value['child'],$area_code);
            }
        }
        return $str;
    }

    /*
     * 根据层级生成分隔符
     * */
    private function getHierarchySeparator($area_code){
        $str = '';
        $num = $this->getPathHierarchy($area_code);

        for($i=0;$i<$num;$i++){
            $str .= $this->separated;
        }
        return $str;
    }

    /*
     * 通过PATH获取当前第几级
     * */
    public function getPathHierarchy($area_code){
        $province = substr($area_code,0,2);//省
        $province .= '0000';
        $city = substr($area_code,0,4);//市
        $city .= '00';
        //$county = $area_code;//县

        if($province==$area_code){
            return 1;
        } else if($city==$area_code) {
            return 2;
        } else {
            return 3;
        }
    }

    /**
     * 删除
     */
    public function del(){
        if(IS_GET){
            $id = I('get.id');
            empty($id) && $this->returnData('请选择操作项！');
            $child = $this->_model->getChildNote($id);
            !empty($child) && $this->returnData('请先删除子节点！');
            if($this->_model->del($id)){
                $this->addHistoryLog('删除成功！');
                $this->clearCache();
                $this->returnData('删除成功！',1);
            }else{
                $this->returnData('删除失败！');
            }
        }else{
            $this->returnData('请求方式错误！');
        }
    }

    /**
     * 清理缓存
     */
    private function clearCache(){
        $this->_model->clearCache();
    }

    public function sort(){
        $id = I('post.id');
        $type = I('post.type');//1=升序，2=降序

        if(empty($id) || !in_array($type,array(1,2))){
            $this->returnData('非法操作');
        }

        $detail = $this->_model->detail($id);

        if(empty($detail)){
            $this->returnData('不存在此数据');
        }

        $res = $this->_model->sort($id,$type);

        if($res){
            $this->addHistoryLog('地区排序修改成功');
            $this->returnData('地区排序修改成功',1);
        } else {
            $this->addHistoryLog('地区排序修改失败');
            $this->returnData('地区排序修改失败');
        }
    }
}
?>